var app = getApp()

Page({

  data: {
    id: null,
    category: null,
    options: null,
    gcwList: [],
    page: 1,
    imageUrl: app.globalData.imageUrl,
    flags: {}
  },

  onLoad: function(options) {
    var that = this
    this.getConfig()
    this.setData({
      page: 1,
      id: options.id,
      options: options,
    })
    wx.showLoading({
      title: '加载数据中...',
      success: () => {
        that.getCategoryInfo()
        that.getGcwList()
      }
    })
  },

  // config
  getConfig: function() {
    var that = this
    wx.request({
      url: app.globalData.url + 'accs/config/v/' + app.globalData.version,
      success: function(res) {
        if (res.data.data) {
          that.setData({
            flags: res.data.data
          })
        }
      }
    })
  },

  onPullDownRefresh: function() {
    wx.showNavigationBarLoading()
    this.onLoad(this.data.options)
  },

  onReachBottom: function() {
    wx.showLoading({
      title: '加载数据中...',
      success: this.getGcwList()
    })
  },

  onShareAppMessage: function() {
    return {
      title: this.data.category.name,
    }
  },

  getCategoryInfo: function() {
    var that = this
    wx.request({
      url: app.globalData.url + 'gcws/get_category_info/id/' + this.data.id,
      success: (res) => {
        res.data.data && that.setData({
          category: res.data.data
        })
      }
    })
  },

  getGcwList: function() {
    var that = this
    var page = this.data.page
    wx.request({
      url: app.globalData.url + 'gcws/get_gcw_list/v/' + app.globalData.version + '/id/' + this.data.id + '/page/' + page,
      success: (res) => {
        var list = res.data.data
        if (list && list.length != 0) {
          that.setData({
            gcwList: page == 1 ? res.data.data : this.data.gcwList.concat(list),
            page: page + 1,
          })
          wx.hideLoading()
        } else {
          wx.hideLoading()
          wx.showToast({
            title: '已经到底了',
            icon: 'success',
            duration: 1000
          })
        }
        wx.hideNavigationBarLoading()
        wx.stopPullDownRefresh()
      }
    })
  },

  gotoDetail: function(e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/detail/detail?id=' + id
    })
  },

})